package com.sinocare.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.common.entity.SystemAccount;
import com.sinocare.common.entity.SystemSettings;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.common.vo.SystemSettingsVo;
import com.sinocare.system.service.SettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/settings")
@Api(tags = "系统设置管理")
@CrossOrigin
public class SettingsController {
    @Resource
    private SettingsService settingsService;

    @ApiOperation(value = "系统设置",notes = "详细描述",httpMethod = "GET")
    @RequestMapping(value = "all",method = RequestMethod.GET)
    public Result findSettings(Integer page,Integer limit,String searchParams) throws Exception{
        final ObjectMapper objectMapper = new ObjectMapper();
        SystemSettingsVo systemSettingsVo=null;
        if(searchParams!=null){
            systemSettingsVo =  objectMapper.readValue(searchParams,SystemSettingsVo.class);
        }
        Page page1= PageHelper.startPage(page, limit);
        List<SystemSettings> settings = settingsService.listSettings(systemSettingsVo);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),settings);
    }

    @ApiOperation(value = "下拉列表动态获取",notes = "详细描述",httpMethod = "POST")
    @RequestMapping(value = "medical",method = RequestMethod.POST)
    public Result query(){
        List<SystemSettingsVo> systemSettingsVos = settingsService.querySettings();
        return Result.ok(ResultCode.SUCCESS,systemSettingsVos);
    }
    @ApiOperation(value = "修改项目",httpMethod = "GET")
    @RequestMapping(value = "updateSettings",method = RequestMethod.GET)
    public Result update(SystemSettings systemSettings){
        int i = settingsService.updateSettings(systemSettings);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,i);
        }
    }
    @ApiOperation(value = "增加项目",httpMethod = "POST")
    @RequestMapping(value = "addSettings",method = RequestMethod.POST)
    public Result add(SystemSettings systemSettings){
        int i = settingsService.addSettings(systemSettings);
        if (i>0){
          return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,i);
        }

    }
}
